package com.example.hao.pickerview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_1:
                showTimePickerView();
                break;
            case R.id.menu_2:
                showOptionsPickerView();
                break;
            case R.id.menu_3:
                break;
            case R.id.menu_4:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showTimePickerView() {


        //时间选择器
        TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                Toast.makeText(MainActivity.this, "" + date, Toast.LENGTH_SHORT).show();
            }
        }).build();
        pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.show();
    }

    public void showOptionsPickerView() {


        final ArrayList<String> provinces = new ArrayList<>();

        provinces.add("福建");
        provinces.add("广东");
        provinces.add("江西");

        final ArrayList<List<String>> citys = new ArrayList<>();
        ArrayList<String> fj = new ArrayList<>();
        fj.add("漳州");
        fj.add("厦门");
        fj.add("福州");
        citys.add(fj);

        ArrayList<String> gd = new ArrayList<>();
        gd.add("深圳");
        gd.add("广州");
        gd.add("东莞");
        citys.add(gd);

        ArrayList<String> jx = new ArrayList<>();
        jx.add("南昌");
        jx.add("吉安");
        jx.add("九江");
        citys.add(jx);

        final ArrayList<List<List<String>>> county = new ArrayList<>();

        for (int i = 0; i < citys.size(); i++) {

            ArrayList<List<String>> lists = new ArrayList<>();
            for (int j = 0; j < citys.size(); j++) {
                ArrayList<String> strings = new ArrayList<>();
                for (int k = 0; k < 5; k++) {
                    strings.add("县城"+i+j+k);
                }
                lists.add(strings);
            }
            county.add(lists);
        }

        //条件选择器
        OptionsPickerView pvOptions = new OptionsPickerBuilder(MainActivity.this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
//                //返回的分别是三个级别的选中位置
                String tx = provinces.get(options1)
                        + citys.get(options1).get(option2)
                        + county.get(options1).get(option2).get(options3);
//                tvOptions.setText(tx);
                Toast.makeText(MainActivity.this, tx, Toast.LENGTH_SHORT).show();
            }
        })
//                .setLayoutRes()
                .setCancelText(" ")

                .build();
        pvOptions.setPicker(provinces, citys, county);
        pvOptions.show();
    }
}
